package com.alfa.myworld.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import com.alfa.myworld.client.GetUsersService;
import com.alfa.myworld.server.domain.User;
import com.alfa.myworld.shared.UserDto;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class GetUsersServiceImpl extends RemoteServiceServlet implements GetUsersService{

	@Override
	public List<UserDto> getUsers() {
		PersistenceManager pm = PMF.get().getPersistenceManager(); 
		Query query = pm.newQuery(User.class);
		List<UserDto> list = new ArrayList<UserDto>();
		try{
			List<User> result = (List<User>) query.execute();			
			for(User user : result){
				UserDto currentUser = new UserDto(); 
				currentUser.setFirstName(user.getFirstName());
				currentUser.setLastName(user.getLastName());
				currentUser.setEmail(user.getEmail());
				currentUser.setBirthday(user.getBirthday());
				list.add(currentUser);
			}
			return list; 
		}finally{
			query.closeAll();
		}
	}

}
